/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.service;

import java.util.List;

import org.hibernate.Session;

import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.service.BaseService;
import etcore.webapp.grouptalk.user.dao.FavouriteDAO;
import etcore.webapp.grouptalk.user.object.Favourite;

/**
 * FavouriteService implements queries and transactions on Favourite
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class FavouriteService extends BaseService {
	/**
	 * Find Favourite instances
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return
	 */
	public List<Favourite> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			List<Favourite> result = dao.findAll(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of Favourite instances
	 * 
	 * @return count of Favourite instances
	 */
	public long findAllSize() {
		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			long result = dao.findAllSize();
			;

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the Favourite instance by the userName and message id
	 * 
	 * @param userName
	 *            userName
	 * @param mid
	 *            message id
	 * @return
	 */
	public Favourite findBy2Properties(String userName, Integer mid) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			Favourite result = dao.findBy2Properties(userName, mid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Favourite instance by id
	 * 
	 * @param id
	 *            Favourite id
	 * @return Favourite instance
	 */
	public Favourite findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			Favourite result = dao.findById(id);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Favourite instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return Favourite instances
	 */
	public List<Favourite> findByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			List<Favourite> result = dao.findByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Favourite instances by a porperty value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Favourite instances
	 */
	public List<Favourite> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			List<Favourite> result = dao.findByProperty(propertyName, value,
					begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Favourite instances displayed in English
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            the count of instances
	 * @return Favourite instances
	 */
	@SuppressWarnings("rawtypes")
	public List findEnglishFavourites(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			List result = dao.findEnglishFavourites(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Favourite instances displayed in the local language
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            the count of instances
	 * @return Favourite instances
	 */
	@SuppressWarnings("rawtypes")
	public List findLocalFavourites(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			List result = dao.findLocalFavourites(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of Favourite instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Favourite instances
	 */
	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			long result = dao.findSizeByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeOfAdminFavourites() {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			long result = dao.findSizeOfAdminFavourites();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findSizeOfAdminFavourites(String propertyName,Object value) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			long result = dao.findSizeOfAdminFavourites(propertyName,value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findSizeOfAdminFavourites(String inputDate) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			long result = dao.findSizeOfAdminFavourites(inputDate);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findSizeOfAdminFavourites(String inputDate,String propertyName,Object value) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			long result = dao.findSizeOfAdminFavourites(inputDate,propertyName,value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	/**
	 * Check whether a Favourite instance with the userName and message id
	 * exists
	 * 
	 * @param userName
	 *            username
	 * @param mid
	 *            message id
	 * @return true or false
	 */
	public boolean isAdded(String userName, Integer mid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FavouriteDAO dao = new FavouriteDAO();
			boolean result = dao.isAdded(userName, mid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
}